using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace RandomRat {

	/// <summary>
	/// Summary description for PatternControl.
	/// </summary>
	public class PatternControl : System.Windows.Forms.UserControl {


		#region WINVARS

		private System.Windows.Forms.GroupBox groupBox1;
		private NumberBox numTolerance;
		private NumberBox numGrouping;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.Button butAdd;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ContextMenu contextMenu2;
		private System.Windows.Forms.MenuItem miRemove;
		private System.Windows.Forms.MenuItem miRename;
		private System.Windows.Forms.MenuItem miClear;
		private System.ComponentModel.Container components = null;

		#endregion


		public PatternControl() {
			InitializeComponent();
			contextMenu2.Popup += new System.EventHandler(this.contextMenu2_Popup);
		}
		public void LoadAdvancedPatternRules(ArrayList advancedPatternRules) {
			// Load the curent paters from parent
			foreach (int[] rule in advancedPatternRules) {
				listView1.Items.Add(new ListViewItem(new string[] {rule[0].ToString(), rule[1].ToString()}));
			}
		}
		public void FillAdvancedPatternRules(ref ArrayList advancedPatternRules) {
			// Clear advancedPatternRules and refill
			advancedPatternRules.Clear();

			foreach (ListViewItem item in listView1.Items) {
				advancedPatternRules.Add(new int[2] {int.Parse(item.SubItems[0].Text), int.Parse(item.SubItems[1].Text)});
			}
		}


		#region UTIL
		protected override void Dispose( bool disposing ) {
			if( disposing ) {
				if(components != null) {
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.numTolerance = new NumberBox();
			this.numGrouping = new NumberBox();
			this.listView1 = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.butAdd = new System.Windows.Forms.Button();
			this.contextMenu2 = new System.Windows.Forms.ContextMenu();
			this.miRemove = new System.Windows.Forms.MenuItem();
			this.miRename = new System.Windows.Forms.MenuItem();
			this.miClear = new System.Windows.Forms.MenuItem();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.numTolerance,
																					this.numGrouping,
																					this.listView1,
																					this.butAdd});
			this.groupBox1.Location = new System.Drawing.Point(1, -1);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(224, 190);
			this.groupBox1.TabIndex = 8;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Rules";
			// 
			// numTolerance
			// 
			this.numTolerance.Location = new System.Drawing.Point(96, 156);
			this.numTolerance.MaxLength = 9;
			this.numTolerance.Name = "numTolerance";
			this.numTolerance.Size = new System.Drawing.Size(66, 20);
			this.numTolerance.TabIndex = 2;
			this.numTolerance.Text = "";
			this.numTolerance.KeyDown += new System.Windows.Forms.KeyEventHandler(this.numBox_KeyDown);
			// 
			// numGrouping
			// 
			this.numGrouping.Location = new System.Drawing.Point(12, 156);
			this.numGrouping.MaxLength = 9;
			this.numGrouping.Name = "numGrouping";
			this.numGrouping.Size = new System.Drawing.Size(85, 20);
			this.numGrouping.TabIndex = 1;
			this.numGrouping.Text = "";
			this.numGrouping.KeyDown += new System.Windows.Forms.KeyEventHandler(this.numBox_KeyDown);
			// 
			// listView1
			// 
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.columnHeader1,
																						this.columnHeader2});
			this.listView1.ContextMenu = this.contextMenu2;
			this.listView1.FullRowSelect = true;
			this.listView1.Location = new System.Drawing.Point(12, 20);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(200, 136);
			this.listView1.Sorting = System.Windows.Forms.SortOrder.Ascending;
			this.listView1.TabIndex = 4;
			this.listView1.View = System.Windows.Forms.View.Details;
			this.listView1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.listView1_KeyDown);
			this.listView1.DoubleClick += new System.EventHandler(this.listView1_DoubleClick);
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Grouping";
			this.columnHeader1.Width = 85;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "Tolerance";
			this.columnHeader2.Width = 85;
			// 
			// butAdd
			// 
			this.butAdd.Location = new System.Drawing.Point(164, 156);
			this.butAdd.Name = "butAdd";
			this.butAdd.Size = new System.Drawing.Size(48, 20);
			this.butAdd.TabIndex = 3;
			this.butAdd.Text = "Add";
			this.butAdd.Click += new System.EventHandler(this.button1_Click);
			// 
			// contextMenu2
			// 
			this.contextMenu2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.miRemove,
																						 this.miRename,
																						 this.miClear});
			// 
			// miRemove
			// 
			this.miRemove.Index = 0;
			this.miRemove.Text = "Remove";
			this.miRemove.Click += new System.EventHandler(this.miRemove_Click);
			// 
			// miRename
			// 
			this.miRename.Index = 1;
			this.miRename.Text = "Edit";
			this.miRename.Click += new System.EventHandler(this.miRename_Click);
			// 
			// miClear
			// 
			this.miClear.Index = 2;
			this.miClear.Text = "Clear";
			this.miClear.Click += new System.EventHandler(this.miClear_Click);
			// 
			// PatternControl
			// 
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.groupBox1});
			this.Name = "PatternControl";
			this.Size = new System.Drawing.Size(228, 192);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion
		#endregion

		

		// Adds rule (Grouping tolorence pair) to the listView (w/ error checking)
		private void addRule() {
			// Check if rule is valid
			bool valid = true;
			
			if (numGrouping.Text == "") {
				valid = false;
				numGrouping.Focus();
			} else if (numTolerance.Text == "") {
				valid = false;
				numTolerance.Focus();
			} else if (!(int.Parse(numGrouping.Text) > 2) || !(int.Parse(numTolerance.Text) > 0)) {
				valid = false;
				MessageBox.Show(this, "Groupings must be greater then 2, and tolerances" + "\n" + 
					"must be greater than 0.");
				if (!(int.Parse(numGrouping.Text) > 2)) {
					numGrouping.SelectAll();
					numGrouping.Focus();
				} else {
					numTolerance.SelectAll();
					numTolerance.Focus();
				}

			} else {
				foreach (ListViewItem item in listView1.Items) {
					if (item.SubItems[0].Text == numGrouping.Text) {
						valid = false;
						MessageBox.Show(this, "A grouping of " + numGrouping.Text + 
							" already exists." + "\n" + "Groupings must be unique.");
						numGrouping.SelectAll();
						numGrouping.Focus();
						break;
					}
				}
			}

			if (valid) {
				listView1.Items.Add(new ListViewItem(new string[] {numGrouping.Text, numTolerance.Text}));
				numGrouping.Text = "";
				numTolerance.Text = "";
				numGrouping.Focus();
			}
		}


		private void editRule() {
			string grouping = listView1.SelectedItems[0].SubItems[0].Text;
			string tolorence = listView1.SelectedItems[0].SubItems[1].Text;
			
			// Show EditRules diolog
			EditRule editBox = new EditRule(grouping, tolorence);
			if (editBox.ShowDialog() == DialogResult.OK) {
				// Check to see if change is valid
				bool valid = true;
				if (editBox.Grouping == null || editBox.Grouping == "" ||
					editBox.Tolerance == null || editBox.Tolerance == "") {
					valid = false;
					MessageBox.Show(this, "A grouping and tolerance must be provided." + "\n" +
						"No changes were made." );

				} else if (!(int.Parse(editBox.Grouping) > 2) || !(int.Parse(editBox.Tolerance) > 0)) {
					valid = false;
					MessageBox.Show(this, "Groupings must be greater then 2, and tolerances must" + "\n" + 
						"be greater than 0.  No changes were made.");

				} else {
					// Iterate through items and see if the grouping already exsists
					foreach (ListViewItem item in listView1.Items) {
						// Ignore the rule being modified
						if (item != listView1.SelectedItems[0]) {
							if (item.SubItems[0].Text == editBox.Grouping) {
								valid = false;
								MessageBox.Show(this, "A grouping of " + editBox.Grouping + 
									" already exists." + "\n" + "Groupings must be unique.");
								break;
							}
						}
					}
				}

				// if valid, update item
				if (valid) {
					listView1.SelectedItems[0].SubItems[0].Text = editBox.Grouping;
					listView1.SelectedItems[0].SubItems[1].Text = editBox.Tolerance;
				}

			}
			editBox.Dispose();
		}

		
		// Alows for editing a rule (Grouping tolorece pair) (w. error cheacking)
		private void listView1_DoubleClick(object sender, System.EventArgs e) {
			editRule();
		}


		// KeyDown event handler for both number boxes 
		private void numBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
			// Adds rule (Grouping tolorence pair) to the listView
			if (e.KeyCode == Keys.Enter) {
				addRule();
			}
		}

		
		// KeyDown event handler for listView1
		private void listView1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
			// Deleate item(s) from listView
			if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back) {
				foreach(ListViewItem item in listView1.SelectedItems) {
					listView1.Items.Remove(item);
				}
			} else if (e.KeyCode == Keys.Enter) {
				editRule();
			}
		}





		// The add button
		private void button1_Click(object sender, System.EventArgs e) {
			addRule();
		}

		private void contextMenu2_Popup(object sender, System.EventArgs e) {
			if (listView1.Items.Count > 0) {
				miClear.Enabled = true;
			} else {
				miClear.Enabled = false;
			}

			if (listView1.SelectedIndices.Count > 0) {
				miRemove.Enabled = true;
				miRename.Enabled = true;
			} else {
				miRemove.Enabled = false;
				miRename.Enabled = false;
			}
		}

		private void miRemove_Click(object sender, System.EventArgs e) {
			foreach(ListViewItem item in listView1.SelectedItems) {
				listView1.Items.Remove(item);
			}
		}

		private void miRename_Click(object sender, System.EventArgs e) {
			editRule();
		}

		private void miClear_Click(object sender, System.EventArgs e) {
			listView1.Items.Clear();
		}

	}
}
